/**
 * 子进程与父进程的内存关系
 * 子进程继承父进程的堆、栈、数据段，共享代码段
*/

#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <unistd.h>

int global_value = 30;

int main()
{
    static int static_value = 30;
    int local_value = 30;

    pid_t pid;
    pid = fork();
    if (0 == pid)
    { //子进程
        global_value = 40, static_value = 40, local_value = 40;
        printf("I am child progress is id %d\tgloba_value address %p"
               " static_value address %p local_value address %p\n",
               getpid(), &global_value, &static_value, &local_value);
        sleep(1);
    }
    else if (0 < pid)
    { //父进程
        global_value = 80, static_value = 80, local_value = 80;
        printf("I am parent progress is id %d\tgloba_value address %p"
               " static_value address %p local_value address %p\n",
               getpid(), &global_value, &static_value, &local_value);
        sleep(1);
    }
    else
    {
        perror("fork error: ");
        exit(EXIT_FAILURE);
    }

    printf("pid is %d\t global_value = %d, static_value = %d, local_value = %d\n",
           getpid(), global_value, static_value, local_value);
    return 0;
}